var Weapon = require("Weapon");
var UIController = require("UIController");
const PlayerController = require("../Player/PlayerController");


var LevelUpSelectButton = cc.Class({
    extends: cc.Component,

    properties: {
        upgradeDescText: cc.Label,
        nameLevelText: cc.Label,

        weaponIcon: cc.Sprite,

        assignedWeapon: Weapon,
    },

    updateButtonDisplay (weapon) {
        if (weapon.node.active == true) {
            this.upgradeDescText.string = weapon.stats[weapon.weaponLevel].upgradeText;
            this.weaponIcon.spriteFrame = weapon.icon;

            this.nameLevelText.string = weapon.name.split('<')[0] + " - Lv " + weapon.weaponLevel;
        } else {
            this.upgradeDescText.string = "Unlock " + weapon.name.split('<')[0];
            this.weaponIcon.spriteFrame = weapon.icon;

            this.nameLevelText.string = weapon.name.split('<')[0];
        }

        this.assignedWeapon = weapon;
    },

    selectUpgrade () {
        if (this.assignedWeapon != null) {
            // 升级已有武器
            if (this.assignedWeapon.node.active == true) {
                this.assignedWeapon.levelUp();
                // 移除已满级武器
                if (this.assignedWeapon.weaponLevel >= this.assignedWeapon.stats.length - 1) {
                    PlayerController.instance.fullLevelWeapons.push(this.assignedWeapon);
                    PlayerController.instance.assignedWeapons.splice(PlayerController.instance.assignedWeapons.indexOf(this.assignedWeapon), 1);
                }
            } else { // 添加新武器
                PlayerController.instance.addWeaponByWeapon(this.assignedWeapon);
            }

            // 这里本应该引用UIController中的levelUpPanel，但是不知道为什么在这个脚本中找不到UIController的单例
            this.node.parent.active = false;

            cc.director.resume();
        }
    },
});

module.exports = LevelUpSelectButton;
